<?php
   require_once "Model.php";
   require_once "Common/ConnDB.php";
   class IndexModel extends Model{
     
   	 public function __construct(){
        parent::__construct();
   	 }
    public function getFoodList()
		{
			/*
			$data = array(
 			array("张三","20","登山","旅游"),
 			array("李四","21","艺术","运动"));
 			*/
 			$sql = "select * from food";
 			$data = $this-> conn -> query($sql);
 			return $data;	
		}
    public function addUserInfo()
    {
      //使用$_POST数组获取表单中输入的数据
        $name = trim($_POST['name']); 
        $sex = $_POST['sex'];
        $password = $_POST['password'];
        
        $phone = $_POST['phone'];

        
        $sql = "insert into user values(null,'$name',$sex,md5('$password'),'$phone')";
        
        

        $result = $this -> conn -> query($sql);//写入数据库
        return $result;
    }
      
      public function doLoginInfo()
      {
        $name = trim($_POST['name']); 
        $password = $_POST['password'];
        $sql="select * from user where name='$name' and password=md5('$password')";
        $result = $this -> conn -> query($sql);
        return $result;
      }

      public function doAdminLoginInfo()
      {
        $name = trim($_POST['adminUser']); 
        $password = $_POST['password'];
        $sql="select * from admin where adminUser='$name' and password=md5('$password')";
        $result = $this -> conn -> query($sql);
        return $result;
      }

      public function getUserByName()
      {
        session_start();
        $name = $_SESSION['user'];
        $sql = "select * from user where name='$name'";
        //echo $sql;
        $data = $this-> conn -> query($sql);
      //var_dump($data);
        return $data;
      }
      public function getOrderByUID()
      {
        session_start();
        $name = $_SESSION['user'];
        $sql = "select * from user where name='$name'";
        //echo $sql;
        $data = $this-> conn -> query($sql);
       // var_dump($data);
        $uid = $data['uid'];
        $sql = "select * from orders where uid={$uid}";

        $data = $this-> conn -> query($sql);
        //var_dump($data);
      //var_dump($data);
        return $data;
      }

    public function updateUserInfo()
    {
        session_start();
        $name1 = $_SESSION['user'];
        $sql = "select * from user where name='$name1'";
        //echo $sql;
        $data = $this-> conn -> query($sql);
        $row1 = $data;
        //var_dump($row1);
        $id = $row1['uid'];
        $name = $row1['name'];
      //使用$_POST数组获取表单中输入的数据 
        $sex = $_POST['sex'];
        $password = $_POST['password'];
        $phone = $_POST['phone'];
        
        $sql = "update user set uid={$id}, name='{$name}', sex={$sex}, password = md5('$password'),phone='{$phone}' where uid={$id}";      
        //echo $sql;
        $result = $this -> conn -> query($sql);//写入数据库
      return $result;
    
    }

    public function addOrderInfo()
    {
      date_default_timezone_set("Asia/Liaoning");
      $totalprice=0;
      session_start();
      $name2 = $_SESSION['user'];
      $sql = "select * from user where name='$name2'";
      $data = $this-> conn -> query($sql);
      $row2 = $data;
        //var_dump($row2);
        $uid = $row2['uid'];
        $numbers = $row2['phone'];
        $fids = implode($_POST['fids'], ',');
        //echo $fids;
        $address = $_POST['address'];
        //echo "</br>";
        //echo $address;
        $times = date("Y-m-d h:i:sa");
        //计算商品总价
        $sql="select * from food";
        $result=mysql_query($sql);
        while($row=mysql_fetch_assoc($result)){
          if(strstr($fids,$row['fid']))
          {
            $totalprice+=$row['price'];
          }
        }
        //echo '</br>';
        //echo $totalprice;
        //echo '</br>';
        $sql = "insert into orders values(null,$uid,'$fids','$numbers',$totalprice,'$address','$times')";
        //echo $sql;
        $result = $this -> conn -> query($sql);//写入数据库
        return $result;
    }

   }

?>